/**
* h5需要与掌证宝app交互时，需引入
* 用法：
* window.zzbNativeComm.exec({
*   funcName: , // 调用app方法名
*   callbackName: , // app主动回调的h5方法名
*   args: , // funcName方法参数
* })
*/
!(function (win) {
  let t = {}
  let execNativeFunc = function (e) {
    e = e || {}
    e.args = e.args || '' // 非e.callbackName类型的传参
    return new Promise((resolve, reject) => {
      if (window.KDS_Native && window.KDS_Native[e.funcName]) {
        if (e.callbackName) {
          // 超时5s报异常
          let timer = setTimeout(() => {
            clearTimeout(timer)
            reject(new Error('app回调异常'))
          }, 5000)
          // 等待app响应原生方法后回调
          window[e.callbackName] = function (data) {
            clearTimeout(timer)
            if (data) {
              resolve({
                code: '0',
                data: data,
                msg: '成功'
              })
            } else {
              reject(new Error('Error!'))
            }
          }
        } else {
          // 只调app原生方法，没有回调
          resolve({
            code: '0',
            data: null,
            msg: '没有给app回调的方法'
          })
        }
        if (e.args) {
        // 兼容其他参数，如
        // 绑定登录：KDS_Native.showBindWin(JSON.stringify({ 'callback': 'upToBuy' }))
        // 获取token：KDS_Native.getNewLoginAuthToken(true, 'getNewToken')
          window.KDS_Native[e.funcName](...e.args)
        } else if (e.callbackName) {
        // 主动调app原生方法
          window.KDS_Native[e.funcName](e.callbackName)
        } else {
        // 主动调app原生方法
          window.KDS_Native[e.funcName]()
        }
      } else {
        resolve({ code: '-1', msg: '方法不存在' })
      }
    })
      // .then(function (results) {
      //   console.log(results)
      //   if (results.code === '0') {
      //     e.callback && e.callback(results.data)
      //   }
      // })
      // .catch(function (err) {
      //   console.log(err)
      // })
  }
  t.exec = function (params) {
    return execNativeFunc(params)
  }
  // t.initExec = function (params) {
  //   console.log('initExec')
  //   let ua = navigator.userAgent.toLowerCase()
  //   if (ua.match(/\(i[^;]+;( u;)? cpu.+mac os x/)) { // ios系统
  //     console.log('mac:', +new Date(), window.app_init)
  //     window.app_init = function () {
  //       execNativeFunc(params)
  //       window.app_init = null
  //     }
  //   } else {
  //     execNativeFunc(params)
  //   }
  // }
  t.otherExec = {
    getAppVersion: () => {
      return new Promise((resolve, reject) => {
        let dzPhoneObj = {}
        let dzPhoneParams = localStorage.getItem('dzPhoneParams') || ''
        // app首次安装时dzPhoneParams获取不到，需
        if (dzPhoneParams === '') {
          // 超时5s报异常
          let timer = setTimeout(() => {
            clearTimeout(timer)
            reject(new Error('app回调setAppVersion异常'))
          }, 5000)

          // 等待app响应原生方法后回调
          window.setAppVersion = (version) => {
            clearTimeout(timer)
            dzPhoneObj.appVersion = version
            resolve(dzPhoneObj)
            // 将setAppVersion置为空
            window.setAppVersion = null
          }
        } else {
          dzPhoneObj = JSON.parse(dzPhoneParams)
          resolve(dzPhoneObj)
        }
      })
    }
  }
  win.zzbNativeComm = t
}(window))
